package cn.wolfcode.web.controller;


import cn.wolfcode.domain.NoticeItem;
import cn.wolfcode.enums.NoticeEnum;
import cn.wolfcode.service.INoticeItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/noticeItem")
public class NoticeItemController {

    @Autowired
    private INoticeItemService noticeItemService;


    @RequiredPermission(name="公告明细页面",expremission="noticeItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<NoticeItem> pageInfo = noticeItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enum", NoticeEnum.values());
        return "noticeItem/list";
    }


    @RequiredPermission(name="公告明细删除",expremission="noticeItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeItemService.delete(id);
        }
        return "redirect:/noticeItem/list";
    }


    @RequiredPermission(name="公告明细新增/编辑",expremission="noticeItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(NoticeItem noticeItem) {
        if (noticeItem.getId() == null) {
            noticeItemService.save(noticeItem);
        } else {
            noticeItemService.update(noticeItem);
        }
        return "redirect:/noticeItem/list";
    }


    @RequiredPermission(name="公告明细新增/编辑",expremission="noticeItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("noticeItem",noticeItemService.get(id));
        }
        return "noticeItem/input";
    }
}
